package com.example.contact_recover;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.contact_recover.models.ServerRes;
import com.example.contact_recover.models.User;
import com.example.contact_recover.util.NetUtil;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BackWdActivity extends AppCompatActivity {

    private BridgeWebView bridgeWebView;
    private String phone;
    private String passwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_back_wd);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        bridgeWebView = findViewById(R.id.wv_back_passwd);
        WebSettings settings = bridgeWebView.getSettings();

        //设置WebView排版算法, 实现单列显示, 不允许横向移动
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);

        //允许访问assets目录
        settings.setAllowFileAccess(true);
        bridgeWebView.loadUrl("file:///android_asset/backpasswd.html");

        bridgeWebView.registerHandler(
                "commitPhone",
                (text, function) -> {
                    // 发送手机验证码....... 这个没有办法实现.. .sms 要钱啊...

                    data data = JSON.parseObject(text, data.class);



                    if (data == null){
                        return ;
                    }

                    if (data.getPasswd() .equals("")|| data.getPhone() .equals("")){
                        Toast.makeText(this,"手机号码| 用户名不能为空",Toast.LENGTH_SHORT).show();
                        return ;
                    }
                    phone = data.getPhone();
                    passwd = data.getPasswd();
                    NetUtil.Commit2Server(NetUtil.SERVER_PULL+"/user/back",data.getPhone(),data.getPasswd(),new commitCall());
                }
        );
    }

    class commitCall implements Callback{

        @Override
        public void onFailure(Call call, IOException e) {

            System.out.println("failure...");
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            System.out.println("response ......");

            byte[] bytes = response.body().bytes();

            if (bytes == null){
                return ;
            }

            String text = new String(bytes, 0, bytes.length);

            ServerRes res = JSON.parseObject(text, ServerRes.class);

            if (res.getCode() == ServerRes.Ok){
                SharedPreferences lastLogin = getSharedPreferences("last_login", MODE_PRIVATE);

                SharedPreferences.Editor editor = lastLogin.edit();

                // todo 这里需要做加密处理
                editor.putString(getResources().getString(R.string.title_phone),phone);
                editor.putString(getResources().getString(R.string.title_passwd),passwd);
                User user = new User();
                System.out.println("u id "+ Long.parseLong(res.getMessage()));
                user.setId(Long.parseLong(res.getMessage()));
                user.setPasswd(passwd);
                user.setPhone(phone);
                user.save();
                editor.putLong("id",user.getId());
                // 保存 登录信息
                editor.apply();

                finish();

                startActivity(new Intent(BackWdActivity.this,MainActivity.class));
            }


        }
    }


}

class data {
    private String phone ;
    private String passwd;

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}
